package cc.mxtt.mvc.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import cc.mxtt.mvc.model.Role;
import cc.mxtt.mvc.model.User;
import cc.mxtt.mvc.model.UserRole;
import cc.mxtt.mvc.service.UserService;

public class UserServiceImpl implements UserService {

	@Override
	public User findById(String id) {
		return User.dao.findById(id);
	}
	@Override
	public List<User> findByIds(String userids){
		String[] ids = userids.split(",");
		String params = "";
		List<Object> paramObj = new ArrayList<Object>();
		for (String s:ids) {
			if ("".equals(params)){
				params="?";
				paramObj.add(s);
			}else{
				params=params+",?";
				paramObj.add(s);
			}
		}
		String sql = "select * from sys_user where id in("+params+")";
		return User.dao.find(sql, paramObj.toArray());
	}
	@Override
	public boolean create(User user) {
		return user.save();
	}

	@Override
	public boolean update(User user) {
		return user.update();
	}

	@Override
	public int delete(String idValues) {
		String[] ids = idValues.split(",");
		String params = "";
		List<Object> paramObj = new ArrayList<Object>();
		for (String s:ids) {
			if ("".equals(params)){
				params="?";
				paramObj.add(s);
			}else{
				params=params+",?";
				paramObj.add(s);
			}
		}
		return Db.update("delete from sys_user where id in ("+params+")",paramObj.toArray());
	}

	@Override
	public int deleteUserRoleByUserId(String idValues){
		String[] ids = idValues.split(",");
		String params = "";
		List<Object> paramObj = new ArrayList<Object>();
		for (String s:ids) {
			if ("".equals(params)){
				params="?";
				paramObj.add(s);
			}else{
				params=params+",?";
				paramObj.add(s);
			}
		}
		//System.out.println(params);
		//System.out.println(paramObj.toArray());
		return Db.update("delete from sys_user_role where user_id in ("+params+")",paramObj.toArray());
	}
	
	@Override
	public boolean addUserRoleByUserIdRoleIds(String userid, String roleids){
		//添加前，先删除该用户已分配的角色
		deleteUserRoleByUserId(userid);
		//遍历数组为用户分配角色
		String[] ids = roleids.split(",");
		for(String roleid:ids){
			UserRole userrole = new UserRole();
			userrole.set("user_id", userid);
			userrole.set("role_id", roleid);
			if(!userrole.save()){
				return false;
			}
		}
		return true;
	}
	@Override
	public List<Role> getListRoleByUserId(String userid){
		String sql = "select a.id,a.name,a.description,IF(ISNULL(b.user_id)=0,true,false) checked";
		sql = sql + " from sys_role a";
		sql = sql + " LEFT JOIN sys_user_role b ON a.id=b.role_id and user_id=?";
		sql = sql + " order by a.name desc";
		return Role.dao.find(sql, userid);
	}
	
	@Override
	public Page<User> getDataGrid(int pageNumber, int pageSize, String username ) {
		String select = "select a.id,a.username,a.nickname,a.mail,a.idcard,a.shouji,a.zuoji,a.dept_id,a.status,b.name dept_name";
		String formwhere = "from sys_user a,sys_dept b where a.dept_id=b.id";
		formwhere = formwhere + " and (a.username like '%"+username+"%' or a.nickname like '%"+username+"%')";
		formwhere = formwhere + " order by a.updatetime desc";
		return User.dao.paginate(pageNumber, pageSize, select, formwhere);
	}
	@Override
	public Page<User> getDataGrid(int pageNumber, int pageSize,
			String username, String deptId) {
		String select = "select a.id,a.username,a.nickname,a.mail,a.idcard,a.shouji,a.zuoji,a.dept_id,a.status,b.name dept_name";
		String formwhere = "from sys_user a,sys_dept b where a.dept_id=b.id";
		formwhere = formwhere + " and (a.username like '%"+username+"%' or a.nickname like '%"+username+"%')";
		formwhere = formwhere + " and (b.parent_id = "+deptId+")";
		formwhere = formwhere + " order by a.updatetime desc";
		return User.dao.paginate(pageNumber, pageSize, select, formwhere);
	}

}
